Generate Files from Templates
Guide to the golo new command for generating Golo source files from templates.
Introduction
The golo new command generates .golo files from embedded templates. Templates are bundled into the binary, so they are always available without external files.
Usage
golo new <template> [--module name] [--name filename] [--function name] [--args a,b,c]
Options
| Option | Description | Default |
|---|---|---|
<template> |
Template name (required) | - |
--module |
Module name | demo |
--name |
Output filename | <template>.golo |
--function |
Function name | main |
--args |
Function arguments (comma-separated) | args |
Quick reference
| Command | Result |
|---|---|
golo new main |
Creates main.golo with module demo, function main |
golo new main --module yolo.Hello --name hello.golo |
Creates hello.golo with module yolo.Hello |
golo new main (file exists) |
Creates main_1.golo (no overwrite) |
golo new main --function greet --args name,age |
Function greet = |name,age| |
golo new (no template) |
Error message + exit 1 |
golo new doesnotexist |
Error message + exit 1 |
Examples
Basic usage
golo new main
Generates main.golo:
module demo
function main = |args| {
println("Hello from demo")
}
Custom module name
golo new main --module yolo.Hello --name hello.golo
Generates hello.golo:
module yolo.Hello
function main = |args| {
println("Hello from yolo.Hello")
}
Custom function and arguments
golo new main --module app.Core --name greet.golo --function greet --args name,age
Generates greet.golo:
module app.Core
function greet = |name,age| {
println("Hello from app.Core")
}
File collision handling
If the output file already exists, golo new will not overwrite it. Instead, it appends an incrementing suffix:
golo new main # Creates main.golo
golo new main # Creates main_1.golo
golo new main # Creates main_2.golo
Error handling
If no template name is provided:
golo new
# Error: template name is required
# Usage: golo new <template> [--module name] [--name filename] [--function name] [--args a,b,c]
If the template does not exist:
golo new unknown
# Error: template 'unknown' not found
Available templates
| Template | Description |
|---|---|
main |
Basic Golo program with a function and a module declaration |
chat.agent |
Chat agent using OpenAI-compatible API with streaming completion |
How templates work
Templates are .golo files stored in the templates/ directory at the root of the project. They are embedded into the binary at build time using Go’s embed package.
Templates use Go template syntax for substitutions:
{{.ModuleName}}- replaced by the module name (--moduleordemo){{.FunctionName}}- replaced by the function name (--functionormain){{.Args}}- replaced by the function arguments (--argsorargs)